package ui;

import controller.CustomerController;
import project_package.Customer;

import java.awt.Component;
import java.awt.event.*;
import java.util.Map;
import java.util.Set;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;

public class CustomerUI implements ActionListener, ListSelectionListener 
{
	private CustomerController custCtrl;	
	private JFrame frmCustomerUi;
	private JPanel addCustomer, editCustomer;
	private JTabbedPane tabbedPane;
	private static CustomerUI instance = null;
	
	private JButton addCustomerButtonAdd;
	private JButton addCustomerButtonReset;
	private JButton addCustomerButtonCloseWindow;
	private JButton addCustomerButtonExit;
	private JComboBox<String> addCustomerComboBoxStatus;
	private JComboBox<Double> addCustomerComboBoxDiscount;
	private JTextField addCustomerTextFieldName;
	private JTextField addCustomerTextFieldPhone;
	private JTextField addCustomerTextFieldAddress;
	private JTextField addCustomerTextFieldCPR;
	
	private JScrollPane editCustomerSP;
	private JList<String> editCustomerList;
	private DefaultListModel<String> listModel;
	private JLabel editCustomerLabelSinceDate;
	private JButton editCustomerButtonUpdate;
	private JButton editCustomerButtonDelete;
	private JButton editCustomerButtonReset;
	private JButton editCustomerButtonExit;
	private JButton editCustomerButtonCloseWindow;
	private JComboBox<String> editCustomerComboBoxStatus;
	private JComboBox<Double> editCustomerComboBoxDiscount;
	private JTextField editCustomerTextFieldName;
	private JTextField editCustomerTextFieldPhone;
	private JTextField editCustomerTextFieldAddress;
	private JTextField editCustomerTextFieldCPR;
	
	public static CustomerUI getInstance()
    {
    	if(instance == null)
    	{
    		instance = new CustomerUI();
    	}
    	return instance;
    }
	public void display()
	{
		frmCustomerUi.getContentPane().add(tabbedPane);
		tabbedPane.add("Add customer", addCustomer);
		tabbedPane.add("Edit customer", editCustomer);
		frmCustomerUi.setSize(400,360);
		frmCustomerUi.setVisible(true);
		frmCustomerUi.setResizable(false);
	}

	public CustomerUI() {
		
		frmCustomerUi = new JFrame("Customer UI");
		frmCustomerUi.setTitle("Customer UI");
		tabbedPane = new JTabbedPane();
		custCtrl = new CustomerController();
		
		fakeCustomerGenerator();

		//**********************************
		// PAGE 1
		//**********************************
		
		addCustomer = new JPanel();

		JLabel addCustomerLabelName = new JLabel("Customer Name");
		JLabel addCustomerLabelPhone = new JLabel("Customer Phone");
		JLabel addCustomerLabelStatus = new JLabel("Customer Status");
		JLabel addCustomerLabelAddress = new JLabel("Customer Address");
		JLabel addCustomerLabelCPR = new JLabel("Customer CPR");
		JLabel addCustomerLabelDiscount = new JLabel("Customer Discount");
	
		addCustomerTextFieldName = new JTextField(32);
		addCustomerTextFieldPhone = new JTextField(16);
		addCustomerTextFieldAddress = new JTextField(25);
		addCustomerTextFieldCPR = new JTextField(10);
		
		addCustomerComboBoxStatus = new JComboBox<String>();
		addCustomerComboBoxStatus.addItem("Normal");
		addCustomerComboBoxStatus.addItem("Business");
		
		addCustomerButtonAdd = new JButton("Add");
		addCustomerButtonAdd.addActionListener(this);
		addCustomerButtonExit = new JButton("Exit");
		addCustomerButtonExit.addActionListener(this);
		addCustomerButtonReset = new JButton("Reset");
		addCustomerButtonReset.addActionListener(this);
		addCustomerButtonCloseWindow = new JButton("Close window");
		addCustomerButtonCloseWindow.addActionListener(this);		
		
		addCustomerComboBoxDiscount = new JComboBox<Double>();
		for(double i = 0; i <= 100; i++) {
			addCustomerComboBoxDiscount.addItem(i);
		}
		GroupLayout gl_addCustomer = new GroupLayout(addCustomer);
		gl_addCustomer.setHorizontalGroup(
			gl_addCustomer.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_addCustomer.createSequentialGroup()
					.addGap(48)
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_addCustomer.createSequentialGroup()
							.addGroup(gl_addCustomer.createParallelGroup(Alignment.LEADING)
								.addComponent(addCustomerLabelPhone)
								.addComponent(addCustomerLabelAddress)
								.addComponent(addCustomerLabelCPR)
								.addComponent(addCustomerLabelStatus)
								.addComponent(addCustomerLabelDiscount)
								.addGroup(gl_addCustomer.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(addCustomerLabelName)))
							.addGap(53)
							.addGroup(gl_addCustomer.createParallelGroup(Alignment.LEADING)
								.addComponent(addCustomerTextFieldCPR, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addCustomerComboBoxStatus, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
								.addComponent(addCustomerTextFieldAddress, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addCustomerComboBoxDiscount, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
								.addComponent(addCustomerTextFieldPhone, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addCustomerTextFieldName, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_addCustomer.createSequentialGroup()
							.addGap(24)
							.addGroup(gl_addCustomer.createParallelGroup(Alignment.LEADING)
								.addComponent(addCustomerButtonAdd, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
								.addComponent(addCustomerButtonCloseWindow))
							.addGap(41)
							.addGroup(gl_addCustomer.createParallelGroup(Alignment.LEADING)
								.addComponent(addCustomerButtonExit)
								.addComponent(addCustomerButtonReset, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(49, Short.MAX_VALUE))
		);
		gl_addCustomer.setVerticalGroup(
			gl_addCustomer.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_addCustomer.createSequentialGroup()
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_addCustomer.createSequentialGroup()
							.addGap(41)
							.addComponent(addCustomerTextFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED))
						.addGroup(Alignment.TRAILING, gl_addCustomer.createSequentialGroup()
							.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(addCustomerLabelName)
							.addGap(9)))
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(addCustomerTextFieldPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addCustomerLabelPhone))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(addCustomerTextFieldAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addCustomerLabelAddress, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(addCustomerTextFieldCPR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addCustomerLabelCPR))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(addCustomerComboBoxDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addCustomerLabelDiscount))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(addCustomerComboBoxStatus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addCustomerLabelStatus))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(addCustomerButtonAdd)
						.addComponent(addCustomerButtonReset))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_addCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(addCustomerButtonCloseWindow)
						.addComponent(addCustomerButtonExit))
					.addContainerGap(49, Short.MAX_VALUE))
		);
		gl_addCustomer.linkSize(SwingConstants.VERTICAL, new Component[] {addCustomerTextFieldName, addCustomerTextFieldPhone});
		gl_addCustomer.linkSize(SwingConstants.HORIZONTAL, new Component[] {addCustomerButtonAdd, addCustomerButtonExit, addCustomerButtonReset, addCustomerButtonCloseWindow});
		gl_addCustomer.linkSize(SwingConstants.HORIZONTAL, new Component[] {addCustomerTextFieldName, addCustomerTextFieldPhone, addCustomerTextFieldAddress, addCustomerTextFieldCPR});
		addCustomer.setLayout(gl_addCustomer);
		
		//**********************************
		// PAGE 2
		//**********************************

		editCustomer = new JPanel();
		
		listModel = new DefaultListModel<String>();
		
		JLabel editCustomerLabelName = new JLabel("Customer Name");
		JLabel editCustomerLabelPhone = new JLabel("Customer Phone");
		JLabel editCustomerLabelStatus = new JLabel("Customer Status");
		JLabel editCustomerLabelAddress = new JLabel("Customer Address");
		JLabel editCustomerLabelCPR = new JLabel("Customer CPR");
		JLabel editCustomerLabelSince = new JLabel("Customer Since");
		JLabel editCustomerLabelDiscount = new JLabel("Customer Discount");
		
		editCustomerTextFieldName = new JTextField(32);
		editCustomerTextFieldName.setEditable(false);
		editCustomerTextFieldPhone = new JTextField(16);
		editCustomerTextFieldAddress = new JTextField(25);
		editCustomerTextFieldCPR = new JTextField(10);

		editCustomerSP = new JScrollPane();
		
		editCustomerComboBoxStatus = new JComboBox<String>();
		editCustomerComboBoxStatus.addItem("Normal");
		editCustomerComboBoxStatus.addItem("Business");
		
		editCustomerButtonUpdate = new JButton("Update");
		editCustomerButtonUpdate.setEnabled(false);
		editCustomerButtonUpdate.addActionListener(this);
		editCustomerButtonDelete = new JButton("Delete");
		editCustomerButtonDelete.setEnabled(false);
		editCustomerButtonDelete.addActionListener(this);
		editCustomerButtonReset = new JButton("Reset");
		editCustomerButtonReset.setEnabled(false);
		editCustomerButtonReset.addActionListener(this);
		editCustomerButtonCloseWindow = new JButton("Close window");
		editCustomerButtonCloseWindow.addActionListener(this);
		editCustomerButtonExit = new JButton("Exit");
		editCustomerButtonExit.addActionListener(this);
		
		updateCustomerList();

		editCustomerLabelSinceDate = new JLabel("n/a");
		
		editCustomerComboBoxDiscount = new JComboBox<Double>();
		for(double i = 0; i <= 100; i++) {
			editCustomerComboBoxDiscount.addItem(i);
		}
		GroupLayout gl_editCustomer = new GroupLayout(editCustomer);
		gl_editCustomer.setHorizontalGroup(
			gl_editCustomer.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_editCustomer.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_editCustomer.createParallelGroup(Alignment.LEADING)
						.addComponent(editCustomerButtonDelete, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
						.addComponent(editCustomerLabelName)
						.addComponent(editCustomerLabelPhone)
						.addComponent(editCustomerLabelAddress)
						.addComponent(editCustomerLabelCPR)
						.addComponent(editCustomerLabelStatus)
						.addComponent(editCustomerLabelDiscount)
						.addComponent(editCustomerLabelSince))
					.addGap(18)
					.addGroup(gl_editCustomer.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_editCustomer.createSequentialGroup()
							.addComponent(editCustomerButtonUpdate)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(editCustomerButtonReset, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))
						.addComponent(editCustomerComboBoxDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(editCustomerComboBoxStatus, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
						.addComponent(editCustomerLabelSinceDate)
						.addComponent(editCustomerTextFieldName, GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
						.addComponent(editCustomerTextFieldPhone)
						.addComponent(editCustomerTextFieldAddress, 0, 0, Short.MAX_VALUE)
						.addComponent(editCustomerTextFieldCPR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_editCustomer.createParallelGroup(Alignment.LEADING)
						.addComponent(editCustomerButtonExit, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(editCustomerButtonCloseWindow, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(editCustomerSP))
					.addGap(112))
		);
		gl_editCustomer.setVerticalGroup(
			gl_editCustomer.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_editCustomer.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_editCustomer.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_editCustomer.createSequentialGroup()
							.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
								.addComponent(editCustomerTextFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(editCustomerLabelName))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
								.addComponent(editCustomerTextFieldPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(editCustomerLabelPhone))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
								.addComponent(editCustomerTextFieldAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(editCustomerLabelAddress))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
								.addComponent(editCustomerTextFieldCPR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(editCustomerLabelCPR))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
								.addComponent(editCustomerComboBoxStatus, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
								.addComponent(editCustomerLabelStatus))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
								.addComponent(editCustomerComboBoxDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(editCustomerLabelDiscount))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
								.addComponent(editCustomerLabelSinceDate)
								.addComponent(editCustomerLabelSince))
							.addGap(57))
						.addGroup(gl_editCustomer.createSequentialGroup()
							.addComponent(editCustomerSP, GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addComponent(editCustomerButtonCloseWindow)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_editCustomer.createParallelGroup(Alignment.BASELINE)
						.addComponent(editCustomerButtonDelete)
						.addComponent(editCustomerButtonUpdate)
						.addComponent(editCustomerButtonReset)
						.addComponent(editCustomerButtonExit, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
					.addGap(34))
		);
		gl_editCustomer.linkSize(SwingConstants.VERTICAL, new Component[] {editCustomerLabelName, editCustomerLabelPhone, editCustomerLabelStatus, editCustomerLabelAddress, editCustomerLabelCPR, editCustomerTextFieldName, editCustomerTextFieldPhone, editCustomerTextFieldAddress, editCustomerTextFieldCPR, editCustomerComboBoxStatus});
		
		editCustomerList = new JList<String>(listModel);
		editCustomerSP.setViewportView(editCustomerList);
		editCustomerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		editCustomerList.setSelectedIndex(0);
		//editCustomerList1.addListSelectionListener(this);
		editCustomerList.setVisibleRowCount(4);
		
		editCustomerList.addListSelectionListener(this);
		editCustomer.setLayout(gl_editCustomer);
				
	}
	
	public void fakeCustomerGenerator() 
	{
		custCtrl.createCustomer("Nikola", "11 111 111", "Aalborg", 1111111111L, 0.9, false);
		custCtrl.createCustomer("Teodor", "22 222 222", "Aalborg", 2222222222L, 0.75, true);
		custCtrl.createCustomer("David", "33 333 333", "Aalborg", 3333333333L, 0.2, false);
		custCtrl.createCustomer("Krizstian", "44 444 444", "Aalborg", 4444444444L, 0.0, true);
		custCtrl.createCustomer("Stefan", "55 555 555", "Aalborg", 5555555555L, 0.2, false);
		for (int i = 0; i < 50; i++)
		{
			custCtrl.createCustomer("Test " + i, String.valueOf(111111111*i), "Somewhere" + i, 5555555555L, 0.0, false);
		}
	}
	
	public void updateCustomerList()
	{
		listModel.clear();
		Set<Map.Entry<Integer, Customer>> st = custCtrl.getCustomers().entrySet();
		 
    	for(Map.Entry<Integer, Customer> customer : st)	{
			listModel.addElement(customer.getValue().getName());
		}
	}
	
	public void customerShow(String customerName) {
		Customer customer = custCtrl.findCustomer(customerName);
		String customerCivilNum = Long.toString(customer.getCivilNum());
		editCustomerComboBoxDiscount.setSelectedItem(customer.getDiscount());
		editCustomerLabelSinceDate.setText(customer.getDate());
		editCustomerTextFieldName.setText(customer.getName());
		editCustomerTextFieldPhone.setText(customer.getPhoneNo());
		editCustomerTextFieldAddress.setText(customer.getAddress());
		editCustomerTextFieldCPR.setText(customerCivilNum);
		editCustomerComboBoxStatus.setSelectedItem(customer.getIsBusiness() ? "Business" : "Normal");
	}
	
	public boolean isDouble(String number) 
	{
		try {
			Double.parseDouble(number);
			return true;
		}
		catch(Exception e) {
			return false;
		}
	}
	
	public boolean isLong(String number) 
	{
		try {
			Long.parseLong(number);
			return true;
		}
		catch(Exception e) {
			return false;
		}
	}
	
    @SuppressWarnings({ "unchecked", "deprecation" })
	public void valueChanged(ListSelectionEvent e) {
    	if (e.getSource() == editCustomerList)
    	{
    		boolean adjust = e.getValueIsAdjusting();
	        if (!adjust)
	        {
	        	editCustomerButtonReset.setEnabled(true);
	    		editCustomerButtonDelete.setEnabled(true);
	    		editCustomerButtonUpdate.setEnabled(true);
	        	JList<String> editCustomerJList1 = (JList<String>) e.getSource();
	        	int selections[] = editCustomerJList1.getSelectedIndices();
	        	Object selectionValues[] = editCustomerJList1.getSelectedValues();
	        	for (int i = 0; i < selections.length; i++) {
	        		customerShow(selectionValues[i].toString());
	        	}
	        }
    	}
    }
    
	public void actionPerformed(ActionEvent e) 
	{
		// Close window
        if((e.getSource() == addCustomerButtonCloseWindow) || (e.getSource() == editCustomerButtonCloseWindow))
        	frmCustomerUi.dispose();
        
        else if ((e.getSource() == addCustomerButtonExit) || (e.getSource() == editCustomerButtonExit))
        	System.exit(0);
        
        else if (e.getSource() == addCustomerButtonAdd) 
        {
        	if (!isLong(addCustomerTextFieldCPR.getText()) || (addCustomerTextFieldCPR.getText().length() != 10))
			{
				JOptionPane.showMessageDialog(addCustomer, "The CPR should be 10 digits","Error", 0, null);
			}
			else {
				String customerName = addCustomerTextFieldName.getText(); 							// name
				String customerPhone = addCustomerTextFieldPhone.getText(); 						// phone
				String customerStatus = addCustomerComboBoxStatus.getSelectedItem().toString();		// status
				String customerAddress = addCustomerTextFieldAddress.getText(); 					// address
				Long customerCivilNum = Long.valueOf(addCustomerTextFieldCPR.getText()); 				// CPR
				double customerDiscount = (double) addCustomerComboBoxDiscount.getSelectedItem();	

				boolean success = custCtrl.createCustomer(customerName, customerPhone, customerAddress, customerCivilNum, customerDiscount, (customerStatus.equals("Business") ? true : false));
				
				if (success)
					JOptionPane.showConfirmDialog(null, "Successfully created customer " + customerName, "Create", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showConfirmDialog(null, "There was error creating the customer", "Create", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
				
				updateCustomerList();
			}
        }
        
        else if (e.getSource() == addCustomerButtonReset)
        {
        	addCustomerTextFieldName.setText("");				// name
			addCustomerTextFieldPhone.setText("");				// phone
			addCustomerComboBoxStatus.setSelectedIndex(0);		// type of worker
			addCustomerTextFieldAddress.setText("");			// address
			addCustomerTextFieldCPR.setText("");				// CPR
        }
        
        else if (e.getSource() == editCustomerButtonUpdate)
        {
        	if (!editCustomerTextFieldName.getText().isEmpty())
			{
        		
        		if (!isLong(editCustomerTextFieldCPR.getText()) || (editCustomerTextFieldCPR.getText().length() != 10))
    			{
    				JOptionPane.showMessageDialog(addCustomer, "The CPR should be 10 digits","Error", 0, null);
    			}
    			else {
					Customer customer = custCtrl.findCustomer(String.valueOf(editCustomerTextFieldName.getText()));
					int customerID = customer.getID();
					String customerName = String.valueOf(editCustomerTextFieldName.getText());
					String customerPhoneNo = String.valueOf(editCustomerTextFieldPhone.getText());
					String customerAddress = String.valueOf(editCustomerTextFieldAddress.getText());
					long customerDatestamp = customer.getDatestamp();
					long customerCivilNum = Long.parseLong(editCustomerTextFieldCPR.getText());
					double customerDiscount = (double) editCustomerComboBoxDiscount.getSelectedItem();	
					boolean customerStatus = (editCustomerComboBoxStatus.getSelectedItem().equals("Normal") ? false : true);
					boolean success = custCtrl.updateCustomer(customerID, customerName, customerPhoneNo, customerAddress, customerCivilNum, customerDatestamp, customerDiscount, customerStatus);
					if (success)
					{
						customerShow(customerName);
						JOptionPane.showConfirmDialog(null, "Successfully updated customer " + editCustomerTextFieldName.getText(), "Update", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
					}
					else
						JOptionPane.showConfirmDialog(null, "There was error updating the customer", "Update", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
					updateCustomerList();
				}
			}
        }
        
        else if (e.getSource() == editCustomerButtonDelete)
        {
        	if (!editCustomerTextFieldName.getText().isEmpty())
			{
				int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + editCustomerTextFieldName.getText() + "?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (response == JOptionPane.YES_OPTION) {
					boolean success = custCtrl.deleteCustomer(editCustomerTextFieldName.getText());
					
					if (success)
						JOptionPane.showConfirmDialog(null, "Successfully deleted customer " + editCustomerTextFieldName.getText(), "Delete", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
					else
						JOptionPane.showConfirmDialog(null, "There was error deleting the customer", "Delete", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
				}
			}
			updateCustomerList();
        }
        
        else if (e.getSource() == editCustomerButtonReset)
        {
        	customerShow(editCustomerTextFieldName.getText());
        }
	}
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		CustomerUI customerMenu = new CustomerUI();
		customerMenu.display();
	}
}
